'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  // 获取数据库表
  const collection = db.collection('pjxx');

  try {
    // 获取数据
    const result = await collection.get();
    const list = result.data; // 数据存储在 `data` 属性中

    // 计算总和，假设字段是 `amount`
    const total = list.reduce((acc, item) => acc + parseFloat(item.je || 0), 0);

    // 返回数据给客户端
    return {
      total: total,
      msg: 'Total calculated successfully'
    };
  } catch (error) {
    console.error('Error fetching data:', error);
    return {
      error: 'Failed to fetch data',
      msg: error.message
    };
  }
};